package com.nongcai159.mall.lib_common.widgets

import android.content.Context
import android.support.v4.view.ViewPager
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.MotionEvent

import com.nongcai159.mall.lib_common.impl.ISlideTopBottom

/**
 * Created by tanbiheng on 2017/10/10.
 */

class DefRecylerView : RecyclerView {
    private var iSlideTopBottom: ISlideTopBottom? = null

    private var viewPager: ViewPager? = null

    fun setISlideTopBotton(iSlideTopBottom: ISlideTopBottom) {
        this.iSlideTopBottom = iSlideTopBottom
    }

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {
        init()
    }

    private fun init() {

    }

    fun setViewPager(viewPager: ViewPager) {
        this.viewPager = viewPager
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (viewPager != null) {
            parent.requestDisallowInterceptTouchEvent(true)
        }
        return super.dispatchTouchEvent(ev)
    }

    override fun onScrolled(dx: Int, dy: Int) {
        var orientation = VERTICAL
        val layoutManager = layoutManager

        when (layoutManager) {
            is LinearLayoutManager -> {
                orientation = layoutManager.orientation
            }
            is GridLayoutManager -> {
                orientation = layoutManager.orientation
            }
        }

        if (orientation == VERTICAL) {
            if (dy > 0) {
                if (!canScrollVertically(-1))
                    iSlideTopBottom?.onSlideBottom()
            } else if (dy < 0) {
                if (!canScrollVertically(1))
                    iSlideTopBottom?.onSlideTop()
            }
        } else {
            if (dx > 0) {
                if (!canScrollHorizontally(-1)) {
                    iSlideTopBottom?.onSlideBottom()
                }
            } else {
                if (!canScrollHorizontally(1)) {
                    iSlideTopBottom?.onSlideTop()
                }
            }
        }
    }

    //    /**
    //     * 是否滑动到底部
    //     */
    //    public boolean isSlideToBottom() {
    //        if (this.getLayoutManager() instanceof LinearLayoutManager) {
    //            LinearLayoutManager layoutManager = (LinearLayoutManager) this.getLayoutManager();
    //            if (layoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {
    //                return this != null
    //                        && this.computeVerticalScrollExtent() + this.computeVerticalScrollOffset()
    //                        >= this.computeVerticalScrollRange();
    //            } else {
    //                return this != null
    //                        && this.computeHorizontalScrollExtent() + this.computeHorizontalScrollOffset()
    //                        >= this.computeHorizontalScrollRange();
    //            }
    //        } else if (this.getLayoutManager() instanceof StaggeredGridLayoutManager) {
    //            StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) this.getLayoutManager();
    //            if (layoutManager.getOrientation() == StaggeredGridLayoutManager.VERTICAL) {
    //                return this != null
    //                        && this.computeVerticalScrollExtent() + this.computeVerticalScrollOffset()
    //                        >= this.computeVerticalScrollRange();
    //            } else {
    //                return this != null
    //                        && this.computeHorizontalScrollExtent() + this.computeHorizontalScrollOffset()
    //                        >= this.computeHorizontalScrollRange();
    //            }
    //        }
    //
    //        return false;
    //    }
}
